package org.example.command;

import java.util.Stack;

public class CommandInvoker {
    private Stack<FarmMachineCommand> commandStack;
    private Stack<FarmMachineCommand> undoStack;

    public CommandInvoker() {
        commandStack = new Stack<>();
        undoStack = new Stack<>();
    }

    public void executeCommand(FarmMachineCommand command) {
        System.out.println("命令调用者: 准备执行命令 - " + command.getCommandName());
        command.execute();
        commandStack.push(command);
        undoStack.clear();
    }

    public void undoCommand() {
        if (!commandStack.isEmpty()) {
            FarmMachineCommand command = commandStack.pop();
            System.out.println("命令调用者: 准备撤销命令 - " + command.getCommandName());
            command.undo();
            undoStack.push(command);
        } else {
            System.out.println("命令调用者: 没有可撤销的命令");
        }
    }

    public void redoCommand() {
        if (!undoStack.isEmpty()) {
            FarmMachineCommand command = undoStack.pop();
            System.out.println("命令调用者: 准备重做命令 - " + command.getCommandName());
            command.execute();
            commandStack.push(command);
        } else {
            System.out.println("命令调用者: 没有可重做的命令");
        }
    }
}